<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">用户名</label>
        <div class="layui-input-block">
            <input id="username" type="text" name="userName" value=""
                   placeholder="请输入用户名"
                   lay-verify="required" lay-reqtext="用户名不能为空"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">昵称</label>
        <div class="layui-input-block">
            <input id="nick" type="nick" name="nick" lay-verify="required" lay-reqtext="昵称不能为空"
                   placeholder="请输入昵称" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">电子邮箱</label>
        <div class="layui-input-block">
            <input id="email" type="email" name="email"
                   lay-verify="required" lay-reqtext="邮箱不能为空"
                   placeholder="请输入邮箱" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">权限</label>
        <div class="layui-input-block">
            <select id="right" name="rightId" lay-filter="right">
                <option value=""></option>
                <option value="1">admin</option>
                <option value="2">default</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">密码</label>
        <div class="layui-input-block">
            <input type="text" name="password" value=""
                   lay-verify="password" lay-reqtext="密码不能为空"
                   class="layui-input" >
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="save">确认保存</button>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../js/jsencrypt.min.js"></script>
<script src="../../js/util.js"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        form.verify({
            password: function (val) {
                if (val.length < 6)
                    return "密码最少六位";
            }
        });

        $("#username").blur(function () {
            $.ajax({
                url: "/user/testUsername",
                data: {username: $("#username").val()},
                type: "POST",
                success: function(data){
                    if (data.content === false) {
                        layer.alert("用户名已存在");
                    }
                },
                error: function(data){
                    layer.alert("用户名验证失败");
                    console.log(data);
                }
            });
        });

        //监听提交
        form.on('submit(save)', function (data) {
            data.field.uid = null;
            data.field.password = encrypted(data.field.password);
            if (!data.field.rightId)
                data.field.rightId = 2;
            $.ajax({
                url: "/user/addUserAdmin",
                method: "POST",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(data.field),
                success: function (res) {
                    if(res.content) {
                        layer.msg("添加成功");
                        setTimeout(function () {
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            parent.tableIns.reload();
                        }, 1000);
                    } else {
                        layer.msg("添加失败");
                    }
                },
                error: function (res) {
                    layer.msg("添加成功");
                    console.error(res);
                }
            });

            return false;
        });

        $("#reset_password").click(function () {
            layer.confirm('确定要重置密码', function (index) {
                var postData = {
                    "uid": uid,
                    "password": encrypted("123456")
                };
                $.ajax({
                    url: "/user/updateUser",
                    method: "POST",
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(postData),
                    success: function (res) {
                        if (res.content) {
                            layer.msg("重置成功");
                        } else {
                            layer.msg("重置失败");
                        }
                        setTimeout(function () {
                            layer.close(index);
                        }, 1000);
                    },
                    error: function (res) {
                        layer.msg("重置失败");
                        setTimeout(function () {
                            layer.close(index);
                        }, 1000);
                    }
                });
            });

        });
    });
</script>
</body>
</html>